﻿using System;

namespace GenericApplication
{

    // 链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    //泛型链表类
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            Node<T> temp = head;
            while (temp.Next != null)
            {
                action(temp.Data);
                temp = temp.Next;
            }
            action(temp.Data);
        }

    }
    class Program
    {

        static void Main(string[] args)
        {
            // 整型List
            GenericList<int> intlist = new GenericList<int>();

            //赋值
            for (int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }

            //打印所有元素
            Console.WriteLine("打印所有的元素：");

            intlist.ForEach(i => Console.WriteLine(i));

            //求最大值
            Console.WriteLine("求最大值：");
            int max = 0;
            intlist.ForEach(delegate (int i) { if (max < i) max = i; });
            Console.WriteLine(max);

            //求最小值
            Console.WriteLine("求最大值：");
            int min = 0;
            intlist.ForEach(delegate (int i) { if (min > i) min = i; });
            Console.WriteLine(min);

            //求和
            Console.WriteLine("求和：");
            int sum = 0;
            intlist.ForEach(i => sum += i);
            Console.WriteLine(sum);


        }

    }
}
